package com.fesine.crm.controller;

import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import com.fesine.crm.exception.CrmException;
import com.fesine.crm.po.EmployeeInfoPo;
import com.fesine.crm.service.CrmFileService;
import com.fesine.crm.service.EmployeeInfoService;
import com.fesine.crm.util.FileUtils;
import com.fesine.dao.model.QueryResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/10/11 21:21
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/11 21:21
 */
@RestController
@RequestMapping("/v1")
public class EmployeeInfoController {

    @Autowired
    private EmployeeInfoService service;

    @Autowired
    @Qualifier("employeeInfoService")
    CrmFileService crmFileService;

    @Value("${user.password}")
    private String password;

    @Value("${temp.filePath}")
    private String tempFilePath;

    @Value("${temp.deleteFlag}")
    private boolean deleteFlag;

    /**
     * 获取员工信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取员工信息", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, dataType = "Integer",
                    paramType = "path")
    })
    @GetMapping("/employee/{id}")
    public Result get(@PathVariable("id") Integer id) {
        EmployeeInfoPo po = new EmployeeInfoPo();
        po.setId(id);
        po = (EmployeeInfoPo) service.get(po);
        if (po != null) {
            return ResultUtils.success(po);
        }
        throw new CrmException(ResultEnum.NOT_FOUND);
    }

    /**
     * 获取员工信息列表
     *
     * @param po
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取员工信息列表", httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", required = true, dataType = "Integer",
                    paramType = "body"),
            @ApiImplicitParam(name = "limit", required = true, dataType = "Integer", paramType
                    = "body"),
            @ApiImplicitParam(name = "po", dataType =
                    "EmployeeInfoPo", paramType = "body")})
    @GetMapping("/employees")
    public Result list(EmployeeInfoPo po
            , @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit) {
        QueryResult<EmployeeInfoPo> list = service.listPage(po, page, limit);
        return ResultUtils.success(list);
    }

    /**
     * 添加员工信息
     *
     * @param po
     * @return
     */
    @ApiOperation(value = "添加员工信息", httpMethod = "POST", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "po", dataType =
                    "EmployeeInfoPo", paramType = "body")
    })
    @PostMapping("/employee")
    public Result save(EmployeeInfoPo po) {
        if (StringUtils.isBlank(po.getPassword())) {
            po.setPassword(password);
        }
        int i = service.save(po);
        if (i == 1) {
            return ResultUtils.success(ResultEnum.CREATED, po);
        }
        throw new CrmException(ResultEnum.INVALID_REQUEST);
    }

    /**
     * 修改员工信息
     *
     * @param po
     * @return
     */
    @ApiOperation(value = "修改员工信息", httpMethod = "PUT", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "po", dataType =
                    "EmployeeInfoPo", paramType = "body")
    })
    @PutMapping("/employee")
    public Result update(EmployeeInfoPo po) {
        //密码需要单独修改，在此过滤密码值，后台将不更新密码
        if (StringUtils.isNotBlank(po.getPassword())) {
            po.setPassword(null);
        }
        int i = service.update(po);
        if (i == 1) {
            return ResultUtils.success(ResultEnum.CREATED, po);
        }
        throw new CrmException(ResultEnum.INVALID_REQUEST);
    }

    /**
     * 单独修改密码
     *
     * @param id
     * @param password
     * @return
     */
    @ApiOperation(value = "修改密码", httpMethod = "PUT", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, dataType = "Integer",
                    paramType = "path"),
            @ApiImplicitParam(name = "password", dataType =
                    "String", paramType = "body")
    })
    @PutMapping("/employee/{id}")
    public Result updatePassword(@PathVariable("id") Integer id, @RequestParam String password) {
        EmployeeInfoPo po = new EmployeeInfoPo();
        po.setId(id);
        po.setPassword(password);
        int i = service.update(po);
        if (i == 1) {
            return ResultUtils.success(ResultEnum.CREATED, id);
        }
        throw new CrmException(ResultEnum.INVALID_REQUEST);
    }

    /**
     * 删除员工信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除员工信息", httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, dataType = "Integer",
                    paramType = "path")
    })
    @DeleteMapping("/employee/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        EmployeeInfoPo po = new EmployeeInfoPo();
        po.setId(id);
        int i = service.delete(po);
        if (i == 1) {
            return ResultUtils.success(ResultEnum.DELETED, id);
        }
        throw new CrmException(ResultEnum.INVALID_REQUEST);
    }

    @DeleteMapping("/employees/{ids}")
    public Result deleteInfos(@PathVariable String ids) {
        String[] idsArr = ids.split(",");
        EmployeeInfoPo po;
        try {
            for (String s : idsArr) {
                po = new EmployeeInfoPo();
                po.setId(Integer.parseInt(s));
                service.delete(po);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw new CrmException(ResultEnum.INVALID_REQUEST);
        }
        return ResultUtils.success(ResultEnum.DELETED, ids);
    }

    /**
     * 导入员工文件
     *
     * @param file
     * @return
     */
    @PostMapping("/employee/file")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
       return FileUtils.importFile(file, tempFilePath, deleteFlag, crmFileService);
    }


}
